﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;

namespace LostLib.Programs
{
	partial class Package
	{
		/// <summary>
		/// Gets installed version of this package.
		/// </summary>
		/// <returns>Installed version or null if nothing is installed.</returns>
		public PackageVersion InstalledVersion
		{
			get
			{
				return (from version in PackageVersions
						where version.Installed
						orderby version.Version descending
						select version).FirstOrDefault();
			}
		}

		/// <summary>
		/// Gets latest suitable version of this package.
		/// </summary>
		/// <returns>Latest suitable verions or null if nothing is installed.</returns>
		public PackageVersion LatestSuitable
		{
			get
			{
				return (from version in PackageVersions
						where version.Suitable
						orderby version.Version descending
						select version).FirstOrDefault();
			}
		}

		/// <summary>
		/// Converts package to string.
		/// </summary>
		/// <returns>String representation of package.</returns>
		public override string ToString()
		{
			return PackageName;
		}
		
		internal static Package Parse(XmlNode packageNode)
		{
			var package = new Package() {
				 PackageName = packageNode.Attributes["name"].Value,
				 Description = packageNode.Attributes["desc"].Value,
			};

			return package;
		}
	}
}
